import requests


class FeishuRobot:
    """
    https://www.feishu.cn/hc/zh-CN/articles/360024984973
    https://open.feishu.cn/document/ukTMukTMukTM/uMDMxEjLzATMx4yMwETM?op_tracking=hc
    """

    def __init__(self, webhook):
        self.webhook = webhook

    def send_card(self, elements: list, header: dict):
        """
        {
            "msg_type": "interactive",
            "card": {
                "config": {
                        "wide_screen_mode": true,
                        "enable_forward": true
                },
                "elements": [{
                        "tag": "div",
                        "text": {
                                "content": "**西湖**，位于浙江省杭州市西湖区龙井路1号，杭州市区西部，景区总面积49平方千米，汇水面积为21.22平方千米，湖面面积为6.38平方千米。",
                                "tag": "lark_md"
                        }
                }, {
                        "actions": [{
                                "tag": "button",
                                "text": {
                                        "content": "更多景点介绍 :玫瑰:",
                                        "tag": "lark_md"
                                },
                                "url": "https://www.example.com",
                                "type": "default",
                                "value": {}
                        }],
                        "tag": "action"
                }],
                "header": {
                        "title": {
                                "content": "今日旅游推荐",
                                "tag": "plain_text"
                        }
                }
            }
        }
        :param elements:
        :param header:
        :return:
        """
        data = {
            "msg_type": "interactive",
            "card": {
                "config": {
                    "wide_screen_mode": True,
                    "enable_forward": True
                },
                "elements": elements,
                "header": header
            }
        }
        return requests.post(url=self.webhook, json=data)

    def send_text(self, text: str):
        """
            {
                "msg_type": "text",
                "content": {
                    "text": "新更新提醒"
                }
            }
        :param text: 发送文本消息
        :return:
        """
        data = {
            "msg_type": "text",
            "content": {
                "text": text
            }
        }
        return requests.post(url=self.webhook, json=data)

    def send_post(self, content: list, language="zh_cn", title="标题"):
        """
        {
                "msg_type": "post",
                "content": {
                        "post": {
                                "zh_cn": {
                                        "title": "项目更新通知",
                                        "content": [
                                                [{
                                                                "tag": "text",
                                                                "text": "项目有更新: "
                                                        },
                                                        {
                                                                "tag": "a",
                                                                "text": "请查看",
                                                                "href": "http://www.example.com/"
                                                        },
                                                        {
                                                                "tag": "at",
                                                                "user_id": "ou_18eac8********17ad4f02e8bbbb"
                                                        }
                                                ]
                                        ]
                                }
                        }
                }
        }
        :param content: 发送富文本消息
        :param language:
        :param title:
        :return:
        """
        data = {
            "msg_type": "post",
            "content": {
                "post": {
                    language: {
                        "title": title,
                        "content": content
                    }
                }
            }
        }
        return requests.post(url=self.webhook, json=data)
